<!--  -->
<template>
  <div class="userWrap">
    <el-row>
      <el-col :span="8">
        <el-input placeholder="请输入内容" v-model="uId">
          <el-button slot="append" type="primary" icon="el-icon-search" @click="searchUser"></el-button>
        </el-input>

        <el-dialog title="收货地址" :visible.sync="dialogTableVisible" :modal="false">
          <el-table :data="gridData">
            <el-table-column property="id" label="编号" width="150"></el-table-column>
            <el-table-column property="rid" label="姓名" width="200"></el-table-column>
            <el-table-column property="username" label="用户名"></el-table-column>
            <el-table-column property="mobile" label="手机号"></el-table-column>
            <el-table-column property="email" label="邮箱"></el-table-column>
          </el-table>
        </el-dialog>
      </el-col>
    </el-row>
    <el-button class="addUser" type="primary" @click="centerDialogVisible = true">添加用户</el-button>

    <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" :modal="false" center>
      <!-- content -->
      <el-radio-group v-model="labelPosition" size="small">
        <el-radio-button label="left">左对齐</el-radio-button>
        <el-radio-button label="right">右对齐</el-radio-button>
        <el-radio-button label="top">顶部对齐</el-radio-button>
      </el-radio-group>
      <div style="margin: 20px;"></div>
      <el-form label-width="80px" :model="addUsers">
        <el-form-item label="username">
          <el-input v-model="addUsers.username"></el-input>
        </el-form-item>
        <el-form-item label="password">
          <el-input v-model="addUsers.password"></el-input>
        </el-form-item>
        <el-form-item label="email">
          <el-input v-model="addUsers.email"></el-input>
        </el-form-item>
        <el-form-item label="mobile">
          <el-input v-model="addUsers.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!-- --- -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <UserTable :userList="userList" @delUser="delUser" @childSend_id="childSend_id" />
    <el-pagination
      background
      layout="prev, pager, next"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :total="1000"
    ></el-pagination>
  </div>
</template>

<script>
import UserTable from "./UserTable";
import {
  getUsersListapi,
  addUserapi,
  delUserapi,
  selectIdapi,
  updataUserapi
} from "@/api/user.js";
export default {
  name: "",
  components: { UserTable },
  data() {
    return {
      dialogTableVisible: false,
      gridData: [
        // {
        //   id: "1",
        //   rid: "1",
        //   username: "1",
        //   mobile:'1',
        //   email:'1'
        // }
      ],
      usersListParams: {
        pagenum: 1,
        pagesize: 10
      },
      uId: "",
      userList: [],
      centerDialogVisible: false,
      labelPosition: "right",
      addUsers: {
        username: "",
        password: "",
        email: "",
        mobile: ""
      }
    };
  },
  methods: {
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.usersListParams.pagenum = val;
      this.getUsersList();
      // console.log(`当前页: ${val}`);
    },

    //请求数据
    getUsersList() {
      getUsersListapi(this.usersListParams).then(res => {
        this.userList = res.data.users;
      });
    },
    //新增用户
    addUser() {
      this.centerDialogVisible = false;
      addUserapi(this.addUsers).then(res => {
        this.getUsersList();
        res.data == 400
          ? this.$message.error("添加失败哦~~")
          : this.$message.success("添加成功");
      });
    },

    // 搜索
    searchUser() {
      selectIdapi(this.uId).then(res => {
        console.log(res);
        if (res.meta.status == 201 || res.meta.status == 200) {
          this.dialogTableVisible = true;
          this.gridData.splice(0, 1, res.data);
          this.$message.success("查询成功");
        } else {
          this.$message.error("id不存在，请重新输入");
          this.gridData.splice(0, 1);
          this.dialogTableVisible = false;
        }
      });
    },

    // 删除用户
    delUser(id) {
      delUserapi(id).then(res => {
        this.getUsersList();
        res.meta.status != 200
          ? this.$message.error("删除失败哦~~")
          : this.$message.success("删除成功");
      });
    },

    //更新信息
    childSend_id(data) {
      console.log(data);
      let params = { mobile: data.mobile, email: data.email };
      updataUserapi(data.id, params).then(res => {
        this.getUsersList();
        res.meta.status != 200
          ? this.$message.error("更新失败哦~~")
          : this.$message.success("更新成功");
      });
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getUsersList();
  },
  mounted() {},
  beforeMount() {}, // 挂载之前
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scoped>
.userWrap {
  width: 100%;
  height: 100%;
  padding: 10px;
  box-sizing: border-box;
}
.addUser {
  float: left;
  margin: 10px 0;
}
</style>  